<?php
/**
 * Created by PhpStorm.
 * User: terry
 * Date: 2018/10/11
 * Time: 下午5:27
 */

namespace AdminBundle\RBAC;


use InvalidArgumentException;

abstract class BaseManager implements ManagerInterface
{
    public $defaultRoles = array();

    /**
     * Returns the named auth item.
     * @param string $name the auth item name.
     * @return Item the auth item corresponding to the specified name. Null is returned if no such item.
     */
    abstract public function getItem($name);

    /**
     * Returns the items of the specified type.
     * @param integer $type the auth item type (either [[Item::TYPE_ROLE]] or [[Item::TYPE_PERMISSION]]
     * @return Item[] the auth items of the specified type.
     */
    abstract protected function getItems($type);

    /**
     * Adds an auth item to the RBAC system.
     * @param Item $item the item to add
     * @return boolean whether the auth item is successfully added to the system
     * @throws \Exception if data validation or saving fails (such as the name of the role or permission is not unique)
     */
    abstract protected function addItem(Item $item);

    /**
     * Adds a rule to the RBAC system.
     * @param Rule $rule the rule to add
     * @return boolean whether the rule is successfully added to the system
     * @throws \Exception if data validation or saving fails (such as the name of the rule is not unique)
     */
    abstract protected function addRule($rule);

    /**
     * Removes an auth item from the RBAC system.
     * @param Item $item the item to remove
     * @return boolean whether the role or permission is successfully removed
     * @throws \Exception if data validation or saving fails (such as the name of the role or permission is not unique)
     */
    abstract protected function removeItem($item);

    /**
     * Removes a rule from the RBAC system.
     * @param Rule $rule the rule to remove
     * @return boolean whether the rule is successfully removed
     * @throws \Exception if data validation or saving fails (such as the name of the rule is not unique)
     */
    abstract protected function removeRule($rule);

    /**
     * Updates an auth item in the RBAC system.
     * @param string $name the name of the item being updated
     * @param Item $item the updated item
     * @return boolean whether the auth item is successfully updated
     * @throws \Exception if data validation or saving fails (such as the name of the role or permission is not unique)
     */
    abstract protected function updateItem($name, $item);

    /**
     * Updates a rule to the RBAC system.
     * @param string $name the name of the rule being updated
     * @param Rule $rule the updated rule
     * @return boolean whether the rule is successfully updated
     * @throws \Exception if data validation or saving fails (such as the name of the rule is not unique)
     */
    abstract protected function updateRule($name, $rule);

    /**
     * @param string $name
     * @return Role
     */
    public function createRole($name)
    {
        $role = new Role();
        $role->name = $name;
        $role->type = Item::TYPE_ROLE;

        return $role;
    }

    /**
     * @param string $name
     * @return Permission
     */
    public function createPermission($name)
    {
        $permission = new Permission();
        $permission->name = $name;
        $permission->type = Item::TYPE_PERMISSION;

        return $permission;
    }

    /**
     * @param Permission|Role|Rule $object
     * @return bool|void
     * @throws \Exception
     */
    public function add($object)
    {
        if ($object instanceof Item) {
            $this->addItem($object);
        } elseif ($object instanceof Rule) {
            $this->addRule($object);
        } else {
            throw new InvalidArgumentException("Adding unsupported object type.");
        }
    }

    public function remove($object)
    {
        if ($object instanceof Item) {
            return $this->removeItem($object);
        } elseif ($object instanceof Rule) {
            return $this->removeRule($object);
        } else {
            throw new InvalidArgumentException("Removing unsupported object type.");
        }
    }

    public function update($name, $object)
    {
        if ($object instanceof Item) {
            return $this->updateItem($name, $object);
        } elseif ($object instanceof Rule) {
            return $this->updateRule($name, $object);
        } else {
            throw new InvalidArgumentException("Updating unsupported object type.");
        }
    }

    public function getRole($name)
    {
        $item = $this->getItem($name);

        return $item instanceof Item && $item->type == Item::TYPE_ROLE ? $item : null;
    }

    public function getPermission($name)
    {
        $item = $this->getItem($name);
        return $item instanceof Item && $item->type == Item::TYPE_PERMISSION ? $item : null;
    }

    public function getRoles()
    {
        return $this->getItems(Item::TYPE_ROLE);
    }

    public function getPermissions()
    {
        return $this->getItems(Item::TYPE_PERMISSION);
    }

    protected function executeRule($user, $item, $params)
    {
        if ($item->getRuleName() === null) {
            return true;
        }
        $rule = $this->getRule($item->getRuleName());
        if ($rule instanceof Rule) {
            return $rule->execute($user, $item, $params);
        } else {
            throw new \Exception("Rule not found: {$item->getRuleName}");
        }
    }
}